package com.gxa.modules.sys.controller;

import com.gxa.common.utils.PageUtils;
import com.gxa.common.utils.RandomString;
import com.gxa.common.utils.Result;
import com.gxa.modules.sys.dto.CdkeyAddDto;
import com.gxa.modules.sys.dto.CdkeyDto;
import com.gxa.modules.sys.entity.Area;
import com.gxa.modules.sys.service.CodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.lang.reflect.Array;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

@Api(tags = "兑换码接口")
@RestController
public class CodeController {

    @Autowired
    private CodeService codeService;

    @Autowired
    private RabbitTemplate rabbitTemplate;




    @ApiOperation(value = "兑换码管理--查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page",value ="当前是第几页",dataType ="int",required = true),
            @ApiImplicitParam(paramType = "query",name = "limit",value ="每页显示多少条",dataType ="int",required = true),
            @ApiImplicitParam(paramType = "query",name = "startTime",value ="查询条件：开始时间",dataType ="Date"),
            @ApiImplicitParam(paramType = "query",name = "endTime",value ="查询条件：结束时间",dataType ="Date"),
            @ApiImplicitParam(paramType = "query",name = "areaId",value ="查询条件：区域id",dataType ="Date"),
            @ApiImplicitParam(paramType = "query",name = "tName",value ="查询条件：类型",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "state",value ="使用情况 ",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "code",value ="兑换码 ",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "platform",value ="平台 ",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "order",value ="升序asc，降序填desc",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "sidx",value ="排序字段",dataType ="String"),

    })
    @GetMapping("/code/list")
    public Result<PageUtils> queryList(@RequestParam @ApiIgnore Map<String,Object> params){

        PageUtils pageUtils = this.codeService.queryList(params);

        return new Result<PageUtils>().ok(pageUtils);
    }
    @ApiOperation(value = "兑换码管理--生成兑换码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "number",value ="生成数量",dataType ="int",required = true),
            @ApiImplicitParam(paramType = "query",name = "platform",value ="使用平台",dataType ="String",required = true),
            @ApiImplicitParam(paramType = "query",name = "startTime",value ="开始时间",dataType ="Date",required = true),
            @ApiImplicitParam(paramType = "query",name = "endTime",value ="结束时间",dataType ="Date",required = true),
            @ApiImplicitParam(paramType = "query",name = "tName",value ="类型",dataType ="String",required = true),
            @ApiImplicitParam(paramType = "query",name = "areaId",value ="区域id",dataType ="int",required = true),

    })
    @PostMapping("/code/add")
    public Result addCode(CdkeyAddDto cdkeyAddDto){

        try {
            this.codeService.addCode(cdkeyAddDto);

            long StartTime = cdkeyAddDto.getStartTime().getTime();
            long  endTime = cdkeyAddDto.getEndTime().getTime();
            Long num1=endTime-StartTime;//时间戳相差的毫秒数
            long num2 =  86400000; //一天的毫秒数
            Integer a = 2147483647; //int最大值是

            if ((num1 + num2) > a ){

                Integer time2 = Math.toIntExact((num1 + num2) / 2); //一半时间

    //            Integer  time2 = 10000;

                MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        message.getMessageProperties().setDelay(time2);
                        return message;
                    }
                };
                this.rabbitTemplate.convertAndSend("exchange-delay","delay-half",time2,messagePostProcessor);
            }else{

                Integer u = Math.toIntExact(num1 + num2); //获取到到期时间 毫秒数
    //            Integer u = 30000;

                String msg = "小于一个月的";
                MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        message.getMessageProperties().setDelay(u);
                        return message;
                    }
                };
                this.rabbitTemplate.convertAndSend("exchange-delay","delay-xx",msg,messagePostProcessor);
            }


            return new Result().ok();
        } catch (Exception e) {
            e.printStackTrace();

            return new Result().error();
        }


    }


    @ApiOperation(value = "兑换接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "code",value ="cdkey代码",dataType ="String",required = true),
            @ApiImplicitParam(paramType = "query",name = "userId",value ="当前用户id",dataType ="int",required = true),

    })
    @PostMapping("/code/cdkey")
    public Result exchange(CdkeyAddDto cdkeyAddDto){

        try {
            String exchange = this.codeService.exchange(cdkeyAddDto);

            return new Result().ok(exchange);
        }catch (Exception e){
            e.printStackTrace();

            return new Result().error();
        }



    }


    @ApiOperation(value = "查询区域接口")
    @GetMapping("/code/area")
    public Result area(){

        try {
            List<Area> areas = this.codeService.queryArea();

            return new Result().ok(areas);
        } catch (Exception e) {
            e.printStackTrace();

            return new Result().error();
        }
    }

    public static void main(String[] args) {
        //生成长度是20的随机字符串
        RandomString gen = new RandomString(20, ThreadLocalRandom.current());
//
//        CdkeyDto cdkeyDto = new CdkeyDto();
//        cdkeyDto.setCdkey(gen.getString());

        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date star = dft.parse("2022-11-01");//开始时间
            Date endDay=dft.parse("2022-11-30");//结束时间
            long starTime=star.getTime();
            long endTime=endDay.getTime();

            long num=endTime-starTime;//时间戳相差的毫秒数
            long nu = 86400000;
           Integer n = Math.toIntExact((num + nu) /2);


            System.out.println(n);
            System.out.println("相差天数为："+num/24/60/60/1000);//除以一天的毫秒数
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



}
